package com.java.base.exercise.iostream.udp;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @Author RenPu
 * @Date 2024/2/3 10:10
 * @Version 1.0
 * @Description:UDP案例协议的编写 -客户端
 **/

@Slf4j
public class UDPClient {

    @SneakyThrows
    public static void main(String[] args) {
        String sentence = "Hello, World!"; // 要发送的消息
        DatagramSocket socket = new DatagramSocket(); // 创建DatagramSocket
        byte[] buffer = sentence.getBytes(); // 将字符串转换为字节数组
        InetAddress address = InetAddress.getByName("localhost"); // 获取目标地址
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 9876); // 创建DatagramPacket
        socket.send(packet); // 发送数据报文
        log.info("Sent message: " + sentence); // 打印发送的消息
    }
}
